//
//  MLAShopFoodCartListView.m
//  MagicLampApp
//
//  Created by Zheng on 2019/4/8.
//  Copyright © 2019 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAShopFoodCartListView.h"

@implementation MLAShopFoodCartListView

- (void)setModel:(MLAShopFoodCartListModel *)model {
    _model = model;
    
    _bottomView.numLbl.text = _model.count;
    _bottomView.priceLbl.text = [NSString stringWithFormat:@"%.2f", _model.total.doubleValue/100.];
    
    CGFloat height = _model.lists.count*40+10;
    if (_model.lists.count > 4) {
        height = 170;
    }
    
    [_tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(height);
        make.bottom.offset(-60);
    }];

    
    [self.tableView reloadData];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.alpha = 0;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
        
        [self addSubview:self.tableView];
        
        _headerView = [[UIView alloc] init];
        _headerView.backgroundColor = [UIColor whiteColor];
        _headerView.layer.cornerRadius = 10;
        _headerView.layer.masksToBounds = YES;
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _closeBtn.frame = CGRectMake(0, 0, 40, 40);
        [_closeBtn setImage:[UIImage imageNamed:@"close_p_icon"] forState:UIControlStateNormal];
        [_headerView addSubview:_closeBtn];
        UILabel *titleLbl = [[UILabel alloc] init];
        titleLbl.frame = CGRectMake(15, 45, 120, 20);
        titleLbl.textColor = CX_GetColorHex(0x999999, 1);
        titleLbl.text = @"已选商品";
        titleLbl.font = CX_GetNormalFont(14);
        [_headerView addSubview:titleLbl];
        [self addSubview:_headerView];
        [self sendSubviewToBack:_headerView];
        [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.tableView.mas_top).offset(10);
            make.left.right.offset(0);
            make.height.offset(90);
        }];
        
        _bottomView = [[MLAShopFoodBottomView alloc] init];
        [self addSubview:_bottomView];
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.offset(0);
            make.height.offset(60);
        }];

    }
    return self;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedSectionFooterHeight = 0.0f;
        _tableView.estimatedSectionHeaderHeight = 0.0f;
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        [self addSubview:_tableView];
        
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
        _tableView.rowHeight = 40;
        [_tableView registerNib:[UINib nibWithNibName:@"MLAShopFoodCartListCell" bundle:nil] forCellReuseIdentifier:@"kCellID"];
        
        
    }
    return _tableView;
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _model.lists.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MLAShopFoodCartListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kCellID" forIndexPath:indexPath];
    MLAShopFoodRightModel *model = _model.lists[indexPath.row];
    cell.model = model;
    
    @weakify(self);
    cell.addClickBlock = ^(MLAShopFoodRightModel * _Nonnull model) {
        @strongify(self);
        self.addClickBlock(model);
    };
    
    cell.subClickBlock = ^(MLAShopFoodRightModel * _Nonnull model) {
        @strongify(self);
        self.subClickBlock(model);
    };

    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}




@end
